package com.gjh.service.cargo.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gjh.dao.cargo.*;
import com.gjh.domain.cargo.*;
import com.gjh.service.cargo.ExportService;
import com.gjh.vo.ExportProductResult;
import com.gjh.vo.ExportResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ExtEproductDao extEproductDao;
    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
        String contractIds = export.getContractIds();
        //将contractId进行切割
        List<String> contractList = Arrays.asList(contractIds.split(","));
        //查询所有货物信息
        ContractProductExample contractProductExample = new ContractProductExample();
        contractProductExample.createCriteria().andContractIdIn(contractList);
        List<ContractProduct> productList = contractProductDao.selectByExample(contractProductExample);//查询出所有的货物

        for (ContractProduct contractProduct : productList) {//遍历货物并保存到 ExportProduct中
            ExportProduct exportProduct = new ExportProduct();
            //利用工具类将类拷贝
            BeanUtils.copyProperties(contractProduct,exportProduct);
            exportProduct.setExportId(export.getId());//保存报运单id
            exportProductDao.insertSelective(exportProduct);

            List<ExtCproduct> extCproductList = contractProduct.getExtCproducts();//查询货物下的所有附件
            for (ExtCproduct extCproduct : extCproductList) {
                ExtEproduct extEproduct = new ExtEproduct();
                //用工具类 拷贝类
                BeanUtils.copyProperties(extCproduct,extEproduct);
                extEproduct.setExportProductId(exportProduct.getId());//保存货物id
                extEproduct.setExportId(export.getId());//保存报运单id
                extEproductDao.insertSelective(extEproduct);
            }
        }

        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andCreateByIn(contractList);
        List<Contract> contracts = contractDao.selectByExample(contractExample);//根据id字符串查询所有合同
        Integer totalProNum = 0;
        Integer totalExtNum = 0;
        StringBuffer sb = new StringBuffer("");
        for (Contract contract : contracts) {
            totalExtNum+=contract.getExtNum();//计算附件总数
            totalProNum+=contract.getProNum();//计算货物总数
            sb.append(contract.getCustomName()).append(" ");//保存所有客户名称，中间以 “ ” 隔开
        }
        export.setProNum(totalProNum);
        export.setExtNum(totalExtNum);
        export.setCustomerContract(sb.toString());
        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        exportDao.updateByPrimaryKeySelective(export);

        List<ExportProduct> exportProducts = export.getExportProducts();
        if(exportProducts!=null&&exportProducts.size()>0) {
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }

        exportDao.updateByPrimaryKeySelective(export);
    }

    @Override
    public void delete(String id) {
        exportDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void updateE(ExportResult exportResult) {
//把海关返回的数据更新到表中，  报运单  报运单货物表
        String exportId = exportResult.getExportId();
        Export export = new Export();
        export.setId(exportId);
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        exportDao.updateByPrimaryKeySelective(export);

        Set<ExportProductResult> products = exportResult.getProducts();
        ExportProduct exportProduct=null;
        for (ExportProductResult product : products) {
            exportProduct =new ExportProduct();
            exportProduct.setId(product.getExportProductId());
            exportProduct.setTax(product.getTax());
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }
    }


}
